import { ImageAsset, size, Vec4 } from 'cc';
import { Texture2D } from 'cc';
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

/**
 * 网格纹理
 */
export class GridTexture {

    img: ImageAsset = null;
    buffer: Uint8Array = null;
    texture: Texture2D = null;
    isDirty = true;
    pixelArr: number[] = [];
    textureSize = size(0, 0);

    // 设置数据
    public initData(w: number, h: number, data?: number[]): Texture2D {
        this.textureSize = size(w, h);

        const _size = size(w, h);
        this.pixelArr = data || this.createPixelArr(w, h);
        if (this.pixelArr.length != _size.width * _size.height) {
            console.error('data.length != _size.width * _size.height');
            return null;
        }

        const len = _size.width * _size.height * 4;
        this.buffer = new Uint8Array(len);

        let pixelIdx = 0;
        for (let i = 0; i < len; i += 4) {
            // x: 控制是否展示
            this.buffer[i] = 255 * this.pixelArr[pixelIdx++];
            this.buffer[i + 1] = 255;
            this.buffer[i + 2] = 255;
            this.buffer[i + 3] = 255;
        }
        this.createTexture(w, h, this.buffer);

        return this.texture;
    }
    // 回收
    public reset() {
        
    }

    // 上传
    public uploadData(): void {
        if (this.isDirty && this.texture) {
            this.isDirty = false;
            // 更新
            this.texture.uploadData(this.buffer);
        }
    }
    // 清除,全部显示
    public clearData() {
        const len = this.textureSize.width * this.textureSize.height * 4;
        for (let i = 0; i < len; i += 4) {
            // x: 控制是否展示
            this.buffer[i] = 255;
            this.buffer[i + 1] = 255;
            this.buffer[i + 2] = 255;
            this.buffer[i + 3] = 255;
        }
        this.isDirty = true;
    }
    /**
     * 更新指定像素位置数据
     * @param x 
     * @param y 
     * @param val :xyzw对应rgba
     * @returns 
     */
    public setData(x: number, y: number, val: Vec4) {
        if (this.pixelArr.length == 0) {
            console.error('pixelArr.length == 0');
            return false;
        }
        const idx = y * this.textureSize.width * 4 + x * 4;

        if (idx >= 0 && idx < this.buffer.length) {
            this.buffer[idx] = val.x * 255;
            this.buffer[idx + 1] = val.y * 255;
            this.buffer[idx + 2] = val.z * 255;
            this.buffer[idx + 3] = val.w * 255;
            this.isDirty = true;
            return true;
        }
        return false;
    }

    // 创建像素数组
    private createPixelArr(w: number, h: number, val = 1) {
        const arr = [];
        const len = w * h;
        for (let i = 0; i < len; i++) {
            arr.push(val);
            // test
            // arr.push(Math.random() > 0.5 ? 1 : 0);
        }
        return arr;
    }
    // 创建纹理
    private createTexture(width, height, buffer) {
        this.img = new ImageAsset();
        this.img.reset({
            _data: buffer,
            width: width,
            height: height,
            format: Texture2D.PixelFormat.RGBA8888,
            _compressed: false,
        });

        this.texture = new Texture2D();
        // 过滤模式
        this.texture.setFilters(Texture2D.Filter.NONE, Texture2D.Filter.NONE);
        // 拼接模式
        this.texture.setWrapMode(Texture2D.WrapMode.CLAMP_TO_EDGE, Texture2D.WrapMode.CLAMP_TO_EDGE);

        this.texture.image = this.img;

        return this.texture;
    }
}


